package com.ft.admix.gdt.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.CSJAdError;
import com.bytedance.sdk.openadsdk.CSJSplashAd;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.mediation.ad.MediationAdSlot;
import com.ft.admix.ad.MixCustomSplashAdapter;
import com.ft.admix.common.MixConstant;
import com.ft.admix.config.AdConfig;
import com.ft.admix.utils.ThreadUtils;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class SplashAdapterLoader extends MixCustomSplashAdapter {
    private CSJSplashAd mSplashAd;

    @Override
    protected void loadNetworkAd(Activity context, AdConfig config) {
        if (!TTAdSdk.isSdkReady()) {

        }
        ThreadUtils.runOnThreadPool(new Runnable() {
            @Override
            public void run() {
                TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(context);
                AdSlot adslot = new AdSlot.Builder()
                        .setCodeId(config.getAdId())
                        .setOrientation(TTAdConstant.VERTICAL)//横竖屏设置
                        .setMediationAdSlot(new MediationAdSlot
                                .Builder()
                                .build())
                        .build();
                adNativeLoader.loadSplashAd(adslot, new TTAdNative.CSJSplashAdListener() {
                    @Override
                    public void onSplashLoadSuccess(CSJSplashAd csjSplashAd) {
                        callLoadSuccess();
                        mSplashAd = csjSplashAd;
                        csjSplashAd.setSplashAdListener(new CSJSplashAd.SplashAdListener() {
                            @Override
                            public void onSplashAdShow(CSJSplashAd csjSplashAd) {
                                callSplashAdShow();
                            }

                            @Override
                            public void onSplashAdClick(CSJSplashAd csjSplashAd) {
                                callSplashAdClicked();
                            }

                            @Override
                            public void onSplashAdClose(CSJSplashAd csjSplashAd, int i) {
                                callSplashAdDismiss();
                            }
                        });
                    }

                    @Override
                    public void onSplashLoadFail(CSJAdError csjAdError) {
                        callLoadFailed(csjAdError.getCode(), csjAdError.getMsg());
                    }

                    @Override
                    public void onSplashRenderSuccess(CSJSplashAd csjSplashAd) {

                    }

                    @Override
                    public void onSplashRenderFail(CSJSplashAd csjSplashAd, CSJAdError csjAdError) {

                    }
                }, 5000);
            }
        });
    }

    @Override
    public void show(ViewGroup container) {
        ThreadUtils.runOnUIThreadByThreadPool(new Runnable() {
            @Override
            public void run() {
                if (mSplashAd!=null&&container!=null){
                    View view = mSplashAd.getSplashView();
                    if (view != null) {
                        ViewParent parent = view.getParent();
                        if (parent instanceof ViewGroup) {
                            ((ViewGroup) parent).removeView(view);
                        }
                        view.setLayoutParams(new
                                ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.MATCH_PARENT));
                        container.removeAllViews();
                        container.addView(view);
                    }
                }
            }
        });
    }

    @Override
    public void destroy() {
        mSplashAd = null;
    }

    @Override
    public MixConstant.AdReadyStatus isReadyCondition() {
        /**
         * 在子线程中进行广告是否可用的判断
         */
        Future<MixConstant.AdReadyStatus> future = ThreadUtils.runOnThreadPool(new Callable<MixConstant.AdReadyStatus>() {
            @Override
            public MixConstant.AdReadyStatus call() throws Exception {
                if (mSplashAd != null) {
                    return MixConstant.AdReadyStatus.AD_IS_READY;
                } else {
                    return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
                }
            }
        });
        try {
            MixConstant.AdReadyStatus result = future.get(500, TimeUnit.MILLISECONDS);//设置500毫秒的总超时，避免线程阻塞
            if (result != null) {
                return result;
            } else {
                return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
    }
}
